<template>
  <div>
    <el-container class="homeHeader">
      <el-header class="title">公司后台管理系统</el-header>
      <el-dropdown class="userInfo">
        <span class="el-dropdown-link">
          {{ user.name }}<i><img :src="user.userFace"></i>
        </span>
        <el-dropdown-menu slot="dropdown" @command="commadnHandler">
          <el-dropdown-item command="userinfo">个人中心</el-dropdown-item>
          <el-dropdown-item command="setting">设置</el-dropdown-item>
          <el-dropdown-item command="logout">登录注销</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    
      <el-container>
        <el-aside width="200px">
          <el-menu router unique-opened>

            <el-submenu 
              :index="index + ''" 
              v-for="(item, index) in this.$router.options.routers" 
              :key="index">

              <template slot="title" v-if="!item.hidden">
                <i :class="item.iconCls" style="color: #1accff;margin-right: 5px;"></i>
                <span>{{ item.name }}</span>
              </template>
                <el-menu-item :index="children.path" 
                              v-for="(children, indexj) in item.children"
                              :key="indexj">
                  {{ children.name }}
                </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 面包屑效果 -->
          <el-breadcrumb separator-class="el-icon-arrow-right" v-if="this.$router.currentRoute.path !== '/home'" class="breadcrumb">
          <!-- 在组件为home的时候不显示面包屑效果 -->
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ this.$router.currentRoute }}</el-breadcrumb-item>
          </el-breadcrumb>
          <div class="homeWelcome" v-if="this.$router.currentRoute.path === '/home'">
            欢迎来到公司后台管理系统
            </div>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
  export default {
    name: 'Home',
    data(){
      return{
        user: JSON.parse(window.sessionStorage.getItem('user'))//拿到内存中的用户信息
      }
    },
    computed:{
      routes(){
        return this.$store.state.routes//拿到routes
      }
    },
    methods: {
      commandHandler(command){
        if(command === 'logout'){
          this.$confirm('登录状态将被注销, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            this.postRequest('/logout')//注销登录
            window.sessionStorage.removeItem('user')
            window.sessionStorage.removeItem('tokenStr')//清空用户信息
            this.$store.commit("initRoutes", [])//清空vuex存储的菜单缓存,每次登录菜单重新加载,如果不做这一步,低级用户可能就可以通过这样访问到高级用户的菜单
            //vuex只有在刷新之后才会清空其中的信息,如果不刷新会导致奇怪的访问
            this.$router.replace('/')//路由跳转
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      }
    }
  }
}

</script>

<style>

.homeHeader{
  background: #409eff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  box-sizing: border-box;
}
.homeHeader .title{
  font-size: 30px;
  font-family: Arial, Helvetica, sans-serif;
  color: white;
}
.homeHeader .userInfo{
  cursor: pointer;
}
.el-dropdown-link img{
  border-radius: 50%;
  width: 48px;
  height: 48px;
  margin-left: 8px;
}
.homeHeader .homeWelcome{
  text-align: center;
  font-size: 30px;
  color: #409eff;
  padding-top: 50px;
  font-family: Arial, Helvetica, sans-serif;
}
.breadcrumb{
  margin-bottom: 10px;
}
</style>